package Project;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

public class ProLogin extends JFrame implements MouseListener {
	JPanel[] pan;
	JButton edubutton, inputbutton, registerbutton;

	JLabel titlelabel;

	DefaultTableModel model1, model2;
	JTable table1, table2;
	JScrollPane spane1, spane2;

	String[] colnames = { "학번", "이름", "학년", "학과" };
	String pro_num;
	JFrame jf = new JFrame();
	JPanel jp_South = new JPanel();
	JPanel jp_West = new JPanel();
	JPanel jp_North = new JPanel();
	JPanel jp_Center = new JPanel();
	
	JPanel jp_sub1 = new JPanel();
	JPanel jp_sub2 = new JPanel();
	
	int row1, column1, row2, column2;
	String sel, sel1;
	JButton btn_edusel = new JButton("선택");
	JButton btn_edudel = new JButton("삭제");
	JButton btn_save = new JButton("저장");

	JLabel lb_input = new JLabel("성적입력 : ");
	JLabel lb_total = new JLabel("등록 가능한 지도학생 리스트");
	JLabel lb_mylist = new JLabel("지도학생 리스트");
	JLabel lb_sub = new JLabel();
	
	JTextField tf_input = new JTextField(5);
	JButton btn_input = new JButton("입력");

	String sub_name;
	
	public ProLogin(Point p, String num) {
		// 교수번호 받아옴
		pro_num = num;
		// 좌표 받아와 셋팅
		jf.setLocation(p);

		titlelabel = new JLabel("교수 로그인", JLabel.CENTER);
		Font font = new Font("dialog", Font.BOLD, 20);
		titlelabel.setFont(font);
		titlelabel.setForeground(new Color(0, 0, 222));

		edubutton = new JButton("지도학생조회");
		inputbutton = new JButton("성적입력");

		DAOTest test = new DAOTest();
		test.OpenDB();
		
		String sql = "select sub_name from subject where sub_num=" +
				"(select pro_subnum from professor where pro_num=?)";
		ResultSet rs;
		try{
			test.pstmt = test.con.prepareStatement(sql);
			test.pstmt.setString(1, pro_num);
			rs = test.pstmt.executeQuery();
			while(rs.next()){
				sub_name = rs.getString("sub_name");
			}
		}catch(Exception x){
			
		}
		
		// 테이블1
		// 설정----------------------------------------------------------------
		model1 = new DefaultTableModel(null, colnames) {
			// 수정 불가능하게 하기
			public boolean isCellEditable(int i, int c) {
				return false;
			}
		};
		table1 = new JTable(model1);
		table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		spane1 = new JScrollPane(table1);

// 테이블2 설정------------------------------------------------------------------
		model2 = new DefaultTableModel(null, colnames) {
			// 수정 불가능하게 하기
			public boolean isCellEditable(int i, int c) {
				return false;
			}
		};
		table2 = new JTable(model2);
		spane2 = new JScrollPane(table2);

// 선택 버튼---------------------------------------------------------------------------
		btn_edusel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Vector temp = new Vector();
				temp.add(table1.getValueAt(row1, 0).toString());
				temp.add(table1.getValueAt(row1, 1).toString());
				temp.add(table1.getValueAt(row1, 2).toString());
				temp.add(table1.getValueAt(row1, 3).toString());

				DAOTest test = new DAOTest();
				test.OpenDB();
				String sql = "update student set stu_pronum=? where stu_num=?";
				try {
					int count = model1.getRowCount();
					for (int i = 0; i < count; i++) {
						test.pstmt = test.con.prepareStatement(sql);
						test.pstmt.setString(1, pro_num);
						String temp_num = model1.getValueAt(i, 0).toString();
						test.pstmt.setString(2, temp_num);
						int n = test.pstmt.executeUpdate();
					}
				} catch (Exception x) {}
				model2.addRow(temp);
				model1.removeRow(row1);
			}
		});

// 삭제 버튼-----------------------------------------------------------------
		btn_edudel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.out.println("삭제");
				Vector temp = new Vector();
				temp.add(table2.getValueAt(row2, 0).toString());
				temp.add(table2.getValueAt(row2, 1).toString());
				temp.add(table2.getValueAt(row2, 2).toString());
				temp.add(table2.getValueAt(row2, 3).toString());

				DAOTest test = new DAOTest();
				test.OpenDB();
				String sql = "update student set stu_pronum = null where stu_num=?";
				try {
					test.pstmt = test.con.prepareStatement(sql);
					String temp1 = model2.getValueAt(row2, 0).toString();
					test.pstmt.setString(1, temp1);
					int n = test.pstmt.executeUpdate();
				} catch (Exception x) {
					x.printStackTrace();
				}
				model1.addRow(temp);
				model2.removeRow(row1);
			}
		});
// 입력 버튼
		btn_input.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String str = tf_input.getText();
				String num = model1.getValueAt(row1, 0).toString();
				DAOTest test = new DAOTest();
				test.OpenDB();
				String sql = "update regist set grade=? " +
						"where reg_subnum=(select pro_subnum from professor where pro_num=?)";
				try{
					test.pstmt = test.con.prepareStatement(sql);
					test.pstmt.setString(1, str);
					test.pstmt.setString(2, pro_num);
					int n = test.pstmt.executeUpdate();
				}catch(Exception x){
					x.printStackTrace();
				}
			}
		});
// 지도학생
		edubutton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 상단 테이블 행 모두 삭제
				int count = model1.getRowCount();
				for (int i = 0; i < count; i++)
					model1.removeRow(0);
				DAOTest test = new DAOTest();
				test.OpenDB();
				String sql = "select * from student where stu_pronum is null";
				ResultSet rs;
				try {
					rs = test.stmt.executeQuery(sql);
					while (rs.next()) {
						System.out.println("while문 수행");
						Vector vec = new Vector();
						vec.add(rs.getString("stu_num"));vec.add(rs.getString("stu_name"));
						vec.add(rs.getString("stu_year"));vec.add(rs.getString("stu_dept"));
						model1.addRow(vec);
					}
				} catch (Exception x) {}
				// 하단 테이블 행 모두 삭제
				int count1 = model2.getRowCount();
				for (int i = 0; i < count1; i++)
					model2.removeRow(0);
				String sql1 = "select * from student where stu_pronum = '"
						+ pro_num + "'";
				ResultSet rs1;
				try {
					rs1 = test.stmt.executeQuery(sql1);
					while (rs1.next()) {
						System.out.println("while문 수행");
						Vector vec = new Vector();
						vec.add(rs1.getString("stu_num"));vec.add(rs1.getString("stu_name"));
						vec.add(rs1.getString("stu_year"));vec.add(rs1.getString("stu_dept"));
						
						model2.addRow(vec);
					}
				} catch (Exception x) {}

				jp_South.remove(lb_input);
				jp_South.remove(tf_input);
				jp_South.remove(btn_input);
				jf.remove(jp_South);

				jp_South.setLayout(new FlowLayout());
				jp_South.add(btn_edusel);
				jp_South.add(btn_edudel);
				
				
				jp_sub1.setLayout(new BoxLayout(jp_sub1, BoxLayout.Y_AXIS));
				jp_sub2.setLayout(new BoxLayout(jp_sub2, BoxLayout.Y_AXIS));
				
				jp_sub1.remove(lb_sub);
				
				jp_sub1.add(lb_total);
				jp_sub1.add(spane1);
				jp_sub2.add(lb_mylist);
				jp_sub2.add(spane2);
				
				jp_Center.setLayout(new GridLayout(2,1));
				jp_Center.add(jp_sub1);
				jp_Center.add(jp_sub2);
				
				jf.add(jp_Center, "Center");
				jf.add(jp_South, "South");
				jf.validate();
			}
		});

// 성적입력 버튼!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		inputbutton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int count2 = table1.getColumnCount();
				if (count2 <= 4)
					model1.addColumn("점수");
				int count1 = model1.getRowCount();
				for (int i = 0; i < count1; i++)
					model1.removeRow(0);
				int count = model1.getRowCount();
				for (int i = 0; i < count; i++)
					model1.removeRow(0);

				DAOTest test = new DAOTest();
				test.OpenDB();
				// 해당 교수의 과목을 가져옴			
				
				String sql = "select stu_num, stu_name, stu_year, stu_dept, grade " +
						"from student, professor, regist " +
						"where student.stu_num = regist.reg_stunum " +
						"and pro_num='" + pro_num + "' " +
						"and professor.pro_subnum = regist.reg_subnum " +
						"and stu_num in(select reg_stunum from regist where reg_subnum in"
						+ "(select pro_subnum from professor where pro_num='"
						+ pro_num + "'))";
				Vector vec1 = new Vector();
				ResultSet rs;
				try {
					rs = test.stmt.executeQuery(sql);
					while (rs.next()) {
						Vector vec = new Vector();
						vec.add(rs.getString("stu_num"));
						vec.add(rs.getString("stu_name"));
						vec.add(rs.getString("stu_year"));
						vec.add(rs.getString("stu_dept"));
						vec.add(rs.getString("grade"));
						
						model1.addRow(vec);
					}
				} catch (Exception x) {x.printStackTrace();}
				
				jp_South.remove(btn_edusel);
				jp_South.remove(btn_edudel);
				jf.remove(jp_South);

				jp_South.setLayout(new GridLayout(1,3));
				jp_South.add(lb_input);
				jp_South.add(tf_input);
				jp_South.add(btn_input);

				jp_Center.setLayout(new BorderLayout());
				jp_Center.remove(jp_sub2);
				jp_Center.remove(jp_sub1);
				jp_sub1.setLayout(new BoxLayout(jp_sub1, BoxLayout.Y_AXIS));
				
				lb_sub.setText("과목 : " + sub_name);
				jp_sub1.remove(lb_total);
				jp_sub1.add(lb_sub);
				jp_sub1.add(spane1);
				
				jp_Center.add(jp_sub1);
				jf.add(jp_South, "South");
				jf.validate();
			}
		});
	}

	public void prologbutton() {
		table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		jp_West.setLayout(new GridLayout(7, 1));
		jp_West.add(new JLabel(""));
		jp_West.add(edubutton);
		jp_West.add(new JLabel(""));
		jp_West.add(inputbutton);
		jp_West.add(new JLabel(""));

		jp_North.add(titlelabel);

		jp_Center.setLayout(new GridLayout(2, 1));

		jp_Center.add(spane1);
		jp_Center.add(spane2);

		jf.add(jp_West, "West");
		jf.add(jp_North, "North");

		jf.add(jp_Center, "Center");
		jf.add(new JLabel(), "South");

		jf.setResizable(false);
		jf.setPreferredSize(new Dimension(600, 500));
		jf.pack();
		jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
		jf.setVisible(true);
	}

	public static void main(String[] args) {
		Point p = new Point();
		p.setLocation(100, 100);
		ProLogin pl = new ProLogin(p, "100");
		pl.prologbutton();
	}

	public void mouseClicked(MouseEvent me) {
		System.out.println("테이블 선택");
		if (me.getSource() == table1) {
			row1 = table1.getSelectedRow();
			column1 = table1.getSelectedColumn();
			sel = table1.getValueAt(row1, 0).toString();
		}
		if (me.getSource() == table1) {
			System.out.println("Table1 선택");
			row2 = table1.getSelectedRow();
			column2 = table1.getSelectedColumn();
			sel1 = table1.getValueAt(row2, 0).toString();
			System.out.println(sel1);
			DAOTest test = new DAOTest();
			test.OpenDB();
		}
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

}
